<?php

namespace App\Http\Traits;

use App\Models\Setting\GlobalSetting;

trait ReferenceNumberTrait
{
    
    protected static function boot() 
    {
        parent::boot();
        static::creating(function($model) {
            $model[self::$referenceNumberField] = self::newNumber();
        });
    }

    public static function newNumber($needUpdateDb = true) {
        $setting = GlobalSetting::find(self::$globalSettingName);
        $number = $setting->increase($needUpdateDb);
        return self::$referenceNumberPrefix . '-' . date('y') . '-' . sprintf('%05d', $number);
    }
}
